/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.thinkgem.jeesite.modules.order.service;

import java.math.BigInteger;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.order.dao.FaHuoDanDao;
import com.thinkgem.jeesite.modules.order.dao.QuHuoDanDao;
import com.thinkgem.jeesite.modules.order.entity.FaHuoDan;
import com.thinkgem.jeesite.modules.order.entity.QuHuoDan;

/**
 * 取货单Service
 * 
 * @author 李忠
 * @version 2013-07-28
 */
@Component
@Transactional(readOnly = true)
public class QuHuoDanService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(QuHuoDanService.class);

	@Autowired
	private QuHuoDanDao quHuoDanDao;
	@Autowired
	private FaHuoDanDao faHuoDanDao;

	public QuHuoDan get(Long id) {
		return quHuoDanDao.findOne(id);
	}

	public Page<QuHuoDan> find(Page<QuHuoDan> page, QuHuoDan quHuoDan) {
		DetachedCriteria dc = quHuoDanDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(quHuoDan.getNo())) {
			dc.add(Restrictions.eq("no", quHuoDan.getNo()));
		}
		if (StringUtils.isNotEmpty(quHuoDan.getShfs())) {
			dc.add(Restrictions.eq("shfs", quHuoDan.getShfs()));
		}

		if (StringUtils.isNotEmpty(quHuoDan.getDangkou())) {
			dc.add(Restrictions.like("dangkou", "%" + quHuoDan.getDangkou() + "%"));
		}
		if (StringUtils.isNotEmpty(quHuoDan.getShouhuoren())) {
			dc.add(Restrictions.like("shouhuoren", "%" + quHuoDan.getShouhuoren() + "%"));
		}

		if (quHuoDan.getQhrUser() != null && quHuoDan.getQhrUser().getId() != null) {
			dc.add(Restrictions.eq("qhrUser.id", quHuoDan.getQhrUser().getId()));
		}
		dc.add(Restrictions.not(Restrictions.eq(QuHuoDan.DEL_FLAG, QuHuoDan.DEL_FLAG_NORMAL)));
		dc.addOrder(Order.desc("id"));
		return quHuoDanDao.find(page, dc);
	}

	@Transactional(readOnly = false)
	public void save(QuHuoDan quHuoDan) {
		quHuoDanDao.save(quHuoDan);
	}

	@Transactional(readOnly = false)
	public void xiadan(QuHuoDan quHuoDan) {
		BigInteger count = quHuoDanDao.countCurrenteDate();
		quHuoDan.setNo(DateFormatUtils.format(new Date(), "yyyyMMdd")
				+ StringUtils.leftPad(String.valueOf(count.longValue() + 1), 4, "0"));
		quHuoDanDao.save(quHuoDan);
	}
	
	@Transactional(readOnly = false)
	public void fenpai(Long qhdUserId, String ids) {
		quHuoDanDao.fenpai(qhdUserId, ids);
	}
	
	@Transactional(readOnly = false)
	public void zhengbao(FaHuoDan faHuoDan) {
		quHuoDanDao.zhengbao(ids);
		faHuoDanDao.save();
	}

	@Transactional(readOnly = false)
	public void delete(Long id) {
		quHuoDanDao.deleteById(id);
	}

}
